﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
using System.Windows.Controls.Primitives;

namespace ControlExplorer.Controls.PdfViewer
{
    public partial class DemoPDFViewer
        : UserControl, IExposeApplicationBarItems
    {
        ApplicationBarIconButton btnZoomIn = new ApplicationBarIconButton(new Uri("/Resources/AppBar/zoomIn.png", UriKind.Relative));
        ApplicationBarIconButton btnZoomOut = new ApplicationBarIconButton(new Uri("/Resources/AppBar/zoomOut.png", UriKind.Relative));

        ApplicationBarIconButton btnNextPage = new ApplicationBarIconButton(new Uri("/Resources/AppBar/arrowDown.png", UriKind.Relative));
        ApplicationBarIconButton btnPrevPage = new ApplicationBarIconButton(new Uri("/Resources/AppBar/arrowUp.png", UriKind.Relative));

        ApplicationBarMenuItem btnShowFindText = new ApplicationBarMenuItem("find text");

        public DemoPDFViewer()
        {
            InitializeComponent();

            btnNextPage.Click += btnNextPage_Click;
            btnNextPage.Text = "next";

            btnPrevPage.Click += btnPrevPage_Click;
            btnPrevPage.Text = "previous";

            btnZoomIn.Click += btnZoomIn_Click;
            btnZoomIn.Text = "zoom in";

            btnZoomOut.Click += btnZoomOut_Click;
            btnZoomOut.Text = "zoom out";

            btnShowFindText.Click += btnShowFindText_Click;

            btnClose_Click(null, new RoutedEventArgs());

            Loaded += DemoPDFViewer_Loaded;

            pdfViewer.RequestNavigate += (s, e) =>
            {
                var result = MessageBox.Show("The document is requesting to navigate to " + e.Uri, "Navigate", MessageBoxButton.OKCancel);
                if (result == MessageBoxResult.OK)
                {
                    // navigate to urls loading web browser control
                    WebBrowser browser = new WebBrowser();
                    browser.Navigate(e.Uri);

                    LayoutRoot.Children.Clear();
                    LayoutRoot.Children.Add(browser);
                }
            };
        }


        void DemoPDFViewer_Loaded(object sender, RoutedEventArgs e)
        {
            var resource = Application.GetResourceStream(new Uri("/ControlExplorer" + App.AssemblySuffix + ";component/Resources/Presentation.pdf", UriKind.Relative));
            pdfViewer.LoadDocument(resource.Stream);

            //pdfViewer.PageRendered += (s2, e2) =>
            {
                //if (e2.TotalRenderedPages >= 2)
                {
                    txtLoading.Visibility = System.Windows.Visibility.Collapsed;
                    pdfViewer.Visibility = System.Windows.Visibility.Visible;
                }
            };
        }

        void btnZoomOut_Click(object sender, EventArgs e)
        {
            pdfViewer.DecreaseZoomCommand.Execute(null);
        }

        void btnZoomIn_Click(object sender, EventArgs e)
        {
            pdfViewer.IncreaseZoomCommand.Execute(null);
        }

        void btnPrevPage_Click(object sender, EventArgs e)
        {
            pdfViewer.PreviousPageCommand.Execute(null);
        }

        void btnNextPage_Click(object sender, EventArgs e)
        {
            pdfViewer.NextPageCommand.Execute(null);
        }

        private void listFiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var file = IsolatedStorageFile.GetUserStoreForApplication();

            var fileName = (string)listFiles.SelectedItem;
            if (fileName != null)
            {
                using (var stream = file.OpenFile(fileName, System.IO.FileMode.Open))
                {
                    this.pdfViewer.LoadDocument(stream);
                    this.pdfViewer.ViewMode = C1.Phone.PdfViewer.ViewMode.Fitted;
                }

                gridViewer.Visibility = Visibility.Visible;
                gridPickFiles.Visibility = Visibility.Collapsed;
            }
        }

        private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            btnDoSearch.IsEnabled = (txtSearch.Text.Trim().Length > 0);
        }

        private void btnDoSearch_Click(object sender, RoutedEventArgs e)
        {
            pdfViewer.FindText = txtSearch.Text;
            pdfViewer.FindNextCommand.Execute(null);
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            panelFindText.Visibility = System.Windows.Visibility.Collapsed;
            btnShowFindText.IsEnabled = true;
        }

        void btnShowFindText_Click(object sender, EventArgs e)
        {
            panelFindText.Visibility = System.Windows.Visibility.Visible;
            btnShowFindText.IsEnabled = false;
        }

        #region IExposeApplicationBarItems Members

        public IEnumerable<IApplicationBarMenuItem> ApplicationBarItems
        {
            get 
            {
                yield return btnZoomOut;
                yield return btnZoomIn;

                yield return btnPrevPage;
                yield return btnNextPage;

                yield return btnShowFindText;
            }
        }

        #endregion

    }
}
